package cn.yuanqiao.archive.danganzhengli.service.impl;

import cn.yuanqiao.archive.appManager.domain.TenantCategory;
import cn.yuanqiao.archive.appManager.domain.TenantMdataList;
import cn.yuanqiao.archive.appManager.mapper.ArchiveMapper;
import cn.yuanqiao.archive.appManager.mapper.TenantCategoryMapper;
import cn.yuanqiao.archive.appManager.mapper.TenantMdataListMapper;
import cn.yuanqiao.archive.appManager.service.ITenantMdataListService;
import cn.yuanqiao.archive.appManager.service.ITenantSysDictTypeService;
import cn.yuanqiao.archive.danganzhengli.mapper.ImportArchiveLogMapper;
import cn.yuanqiao.archive.danganzhengli.service.IArchiveDBFService;
import cn.yuanqiao.archive.danganzhengli.utils.DBFReadUtils;
import cn.yuanqiao.archive.wenjianzhengli.utils.UploadDAO;
import cn.yuanqiao.common.core.domain.model.LoginUser;
import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.ListUtils;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.system.service.ISysDeptService;
import cn.yuanqiao.system.service.ISysUserService;
import com.alibaba.fastjson2.JSONObject;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;
import java.util.Map;

@Service
public class ArchiveDBFServiceImpl implements IArchiveDBFService {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private ITenantMdataListService mdataListService;
    @Autowired
    private TenantCategoryMapper categoryMapper;
    @Autowired
    private ImportArchiveLogMapper importArchiveLogMapper;
    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;
    @Autowired
    private ITenantSysDictTypeService sysDictTypeService;
    @Autowired
    private TenantMdataListMapper mdataListMapper;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private ArchiveMapper archiveMapper;

    @Override
    public JSONObject readDBFTitle(File file,Integer type,Long categoryId) throws Exception{
        JSONObject object = new JSONObject();
        object.put("xlsTitle", DBFReadUtils.parseDBFTitle(file));  //表格表头
        TenantMdataList mdataList = new TenantMdataList();
        mdataList.setIsImport(1);
        mdataList.setType(type);
        object.put("sysTitle", mdataListService.selectTenantMdataListListByCategoryId(mdataList, categoryId));  //可导入字段
        return object;
    }

    @Override
    public UploadDAO saveDBFFile(JSONObject data,File dbfFile) throws Exception{
        if(!data.containsKey("categoryId") || StringUtils.isEmpty(data.getString("categoryId"))
                && !data.containsKey("type") || StringUtils.isEmpty(data.getString("type"))
                && !data.containsKey("colNames") || StringUtils.isEmpty(data.getString("colNames")))
        {
            throw new ServiceException("参数错误！");
        }
        data.put("JS_TYPE","6");
        data.put("STATE","12");
        TenantCategory category = categoryMapper.selectTenantCategoryByID(data.getLong("categoryId"),new TenantCategory().getTableName());
        if(category.getType() == 3 && data.getInteger("type") == 1
                && (!data.containsKey("itemId") || StringUtils.isEmpty(data.getString("itemId"))))
        {
            data.put("itemId",null);
            //throw new ServiceException("参数错误！");
        }
        if((category.getType() == 3 || category.getType() == 1) && data.getInteger("type") == 0
                && (!data.containsKey("folderId") || StringUtils.isEmpty(data.getString("folderId"))))
        {
            data.put("folderId",null);
            //throw new ServiceException("参数错误！");
        }
        List<Map<Integer, String>> list = DBFReadUtils.parseDBF(dbfFile);
        List<List<Map<Integer, String>>> newList = ListUtils.splitListIndependent(list,1000);//每次处理1000行
        UploadDAO uploadDAO = new UploadDAO(); //解析入库在这里面完成
        long beginTime = System.currentTimeMillis();
        LoginUser user = SecurityUtils.getLoginUser();
        uploadDAO.setType(1); //解析入库
        uploadDAO.setLoginUser(user);
        uploadDAO.setCategory(category);  //门类
        uploadDAO.setSqlSessionTemplate(sqlSessionTemplate);  //数据连接
        uploadDAO.setSysDictTypeService(sysDictTypeService); // 字典
        uploadDAO.setUserService(userService);
        uploadDAO.setDeptService(deptService);
        uploadDAO.setArchiveMapper(archiveMapper);
        uploadDAO.setData(data);
        uploadDAO.setCreateTime(DateUtils.getTime());
        TenantMdataList mdataList = new TenantMdataList();
        mdataList.setType(data.getInteger("type"));
        uploadDAO.setMdataList(mdataListMapper.selectNotEmptyDefaultByCategoryId(mdataList,data.getLong("categoryId"),new TenantCategory().getTableName()));
        for(int i = 0;i<newList.size();i++){
            logger.info("dbf文件解析第"+(i*1000)+"行处理开始！");
            uploadDAO.save(newList.get(i));
        }
        long endTime = System.currentTimeMillis();
        uploadDAO.setTime(endTime - beginTime);
        return uploadDAO;
    }
}
